package com.example.tests;

//import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactCreationTests extends TestBase {
	
	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws Exception {
		List<Object[]> list = wrapContactDataForProvider(loadContactsFromXmlFile(new File("contacts.xml")));
		return list.iterator();				
	}	

	@Test(dataProvider = "contactsFromFile")
	public void testContactCreationWithValidData(ContactData contact) throws Exception {
		//save old contacts
		SortedListOf<ContactData> oldContacts = app.getContactHelper().getContacts();
		
		//action
		app.getContactHelper().createContact(contact);
		
		//save new contacts
		SortedListOf<ContactData> newContacts = app.getContactHelper().getContacts();
		
		// assertion
		assertThat(newContacts, equalTo(oldContacts.withAdded(contact)));
	}
	  
	@Test(dataProvider = "invalidContactGenerator")
	public void testUnsuccessfulContactCreationWithInvalidData(ContactData contact) throws Exception {
		//save old contacts
		SortedListOf<ContactData> oldContacts = app.getContactHelper().getContacts();
		
		//action
		app.getContactHelper().createContact(contact);
		
		//save new contacts
		SortedListOf<ContactData> newContacts = app.getContactHelper().getContacts();
		
		// assertion
		assertThat(newContacts, equalTo(oldContacts));
	}
}
